package hk.edu.polyu.comp.motioncapture;

import hk.edu.polyu.comp.motioncapture.point.*;

/**
 * 
 * @author Will
 * @deprecated
 * 
 */
public class CommandCheck {

	/**
	 * @param points
	 *            A Point array to be recognized as command.
	 */
	public int getCommand(Point[] points) {

		if (points == null) {
			System.out.print("null points");
			return 0;
		}
		// For 3 points, move
		if (points.length == 3) {
			int min_id = 0, n;
			double min_len = 30000, len;
			double[] slopes = new double[3];

			for (int m = 0; m < 3; m++) {
				n = (m != 2 ? m + 1 : 0);
				len = Math.pow(points[m].getX() - points[n].getX(), 2)
						+ Math.pow(points[m].getY() - points[n].getY(), 2);
				slopes[m] = (points[m].getY() - points[n].getY())
						/ (points[m].getX() - points[n].getX());
				if (len < min_len) {
					min_len = len;
					min_id = m;
				}
			}
			int fst = min_id;
			int sec = (min_id != 2 ? min_id + 1 : 0);
			int trd = (min_id >= 1 ? min_id - 1 : min_id + 2);

			double slope = slopes[min_id];

			// System.out.println("Min: " + min_id);
			// System.out.println("Slope: " + slope);
			// double deg = (slope[max_id] == 0? 0: Math.atan(-1.0 /
			// slope[max_id]));
			// System.out.println("Degree: " + deg);

			if (Math.abs(slope) <= 1) // Move forward or backward
			{
				if ((points[fst].getY() + points[sec].getY()) / 2 > points[trd]
						.getY()) // Move forward
				{
					// System.out.println("Forward");
					return 1;
				} else // Move backward
				{
					// System.out.println(points[fst].y);
					// System.out.println(points[sec].y);
					// System.out.println(points[trd].y);
					// System.out.println("Backward");
					return 2;
				}
			} else // Move left or right
			{
				if ((points[fst].getX() + points[sec].getX()) / 2 > points[trd]
						.getX()) // Move left
				{
					// System.out.println("Left");
					return 4;
				} else // Move right
				{
					// System.out.println("Right");
					return 3;
				}
			}

		}

		// For 2 points, fire
		if (points.length == 2)
			return 5;

		// For 0 point, idle
		if (points.length == 0)
			return 0;

		// Error
		return -1;
	}

}
